package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dk.VO.CarsVO;
import com.dk.constant.CollectionConstant;
import com.dk.mapper.TbCarsMapper;
import com.dk.page.PageResult;
import com.dk.pojo.TBCars;
import com.dk.service.CarsService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class CarsServiceImpl implements CarsService {
    @Autowired
    public TbCarsMapper tbCarsMapper;
    @Autowired
    public RedisTemplate redisTemplate;

    @Override
    public List<TBCars> getAll() {
        List<TBCars> list = new ArrayList<>();
        Set<String> set = redisTemplate.boundZSetOps(CollectionConstant.POPULAR_LIST).reverseRange(0, -1);
        if (set != null && set.size() != 0) {
            for (String s : set) {
                Long id = Long.valueOf(s);
                TBCars o = (TBCars) redisTemplate.boundHashOps(CollectionConstant.CAR).get(CollectionConstant.CAR_ID + "-" + id);
                list.add(o);
            }

        }
        tbCarsMapper.selectAll();
        return list;
    }

    @Override
    public void getTime() {
        LambdaQueryWrapper<TBCars> lwq = new LambdaQueryWrapper<>();
        lwq.orderBy(true,false,TBCars::getStarttime);
        List<TBCars> carsList = tbCarsMapper.selectList(lwq);
        if (carsList != null && carsList.size() != 0){
            for (TBCars cars1 : carsList) {
                Long s=cars1.getId();
                String id = String.valueOf(s);
                redisTemplate.boundHashOps(CollectionConstant.CAR).put(CollectionConstant.CAR_ID+"-"+cars1.getId(),cars1);
                redisTemplate.boundListOps(CollectionConstant.NEW_ARRIVAL).rightPush(id);

            }
        }
    }

    @Override
    public List<TBCars> getPutaWay() {
        List<TBCars> list = new ArrayList<>();
        List<String> range = redisTemplate.boundListOps(CollectionConstant.NEW_ARRIVAL).range(0,-1);
        if (range !=null && range.size() !=0){
            for (String s:range){
                Long id = Long.valueOf(s);
                TBCars o = (TBCars) redisTemplate.boundHashOps(CollectionConstant.CAR).get(CollectionConstant.CAR_ID + "-" + id);
                list.add(o);
            }
        }
        return list;
    }


}
